angular.module('imsApp').controller('appController', ['$scope', '$rootScope', '$state', 'AppManager','$sce','ImsDialog', function ($scope, $rootScope, $state, AppManager,$sce,ImsDialog) {

    $scope.search = {pageNo:$state.params.pageNo || 1,pageSize:$state.params.pageSize||10,
        keyword:$state.params.keyword,appType:$state.params.appType,status:$state.params.status,channelId:$state.params.channelId};


    $scope.appType={38:$scope.table[38],39:$scope.table[39],40:$scope.table[40],41:$scope.table[41]};
    $scope.status={1:$scope.table[1],2:$scope.table[2],5:  $scope.table[5]}
    $scope.keyword=$scope.search.keyword

    $scope.paginationConf = {
        currentPage: $scope.search.pageNo,
        itemsPerPage: $scope.search.pageSize,
        pagesLength: 9,
        search: false,
        onChange: function(){
            $scope.search.pageNo = this.currentPage;
            $scope.search.pageSize = this.itemsPerPage;
            $state.go($state.current.name,$scope.search);

        }
    };
    $scope.searchChange=function () {
        $scope.search.pageNo=1;
        $state.go($state.current.name,$scope.search);
    }

    $scope.searchClick=function (keyword) {
        $scope.search.pageNo=1;
        $scope.search.keyword=keyword;
        $state.go($state.current.name,$scope.search);

    }


    $scope.deletes = function (key) {
        $scope.search[key]=null;
        $scope.search.pageNo=1;
        $state.go($state.current.name,$scope.search);
    }
    
    
   $scope.deleteApps=function () {
       var ids=[];
       $scope.lists.forEach(function (item) {
           if(item.checked){
               ids.push(item.appId)
           }
       })
       if(!ids.length){
           return false;
       }else{
           ImsDialog.open({title: "删除确认", message: "您确定要删除数据吗?"}).then(function (a) {
               if (a == 'ok') {
                   AppManager.appDelete(ids).then(function (result) {
                       if(result.data.c=='s'){
                           $state.reload();
                       }
                   })
               }
           })
       }

       
   }
    AppManager.appList([$scope.search.pageNo,$scope.search.pageSize],$scope.search).then(function (result) {
        $scope.lists = result.data.pageList;
        for(var i=0; i< $scope.lists.length;i++){
            $scope.lists[i].shortName = $sce.trustAsHtml($scope.lists[i].shortName);
        }
        $scope.paginationConf.totalItems = result.data.total;
    })

    $scope.appChangeStatus=function (app) {
        var status="";
        if(app.status==1){
            status=5
        }else if(app.status==2){
            status=1
        }else {
            status=2

        }
        AppManager.appStatusEdit(app.appId,{status:status}).then(function (result) {
            console.log(result)
          if(result.data.c=='s'){
              $state.reload()
          }
        })
    }


}])

